import { useState, useEffect } from "react";

export const useDebounce = <T>(value: T, delay?: number) => {
  const [debouncedValue, setDebouncedValue] = useState(value);

  useEffect(() => {
    const timeout = setTimeout(() => setDebouncedValue(value), delay);
    return () => clearTimeout(timeout);
  }, [value, delay]);

  return debouncedValue;
};

// const debounce = (func, delay) => {
//   let timeout;
//   return (...args) => {
//     if (timeout) {
//       clearTimeout(timeout);
//     }
//     timeout = setTimeout(function () {
//       func(...args);
//     }, delay);
//   };
// };
// const log = debounce(() => console.log("防抖"), 5000);
// log();
